package model.feature;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.Container;
import model.Player;
import model.Settings;
import model.contents.Content;
import model.contents.ContentType;
import model.contents.TextContent;
import util.PlayerUtilities;
import view.content.DateBox;
import view.content.WeatherBox;

public class FeaturePlayer {

	private static FeaturePlayer _instance;
	//private static Map<Integer, FeatureItems> featureItems;

	static
	{
		//featureItems = FeatureItems.getInstance().getFeatureItemsList();
	}

	public static FeatureDefinition definition;

	public static FeaturePlayer getInstance() {
		//if (_instance == null) {
			_instance = new FeaturePlayer();
			//_instance.definition = FeatureDefinition.getInstance();
		//}
		return _instance;
	}

	private FeaturePlayer()
	{
		loadFeatureDefnition();
		//this.featureItems = new HashMap<Integer, FeatureItems>();
	}

	public void update() {
		this.loadFeatureDefnition();
		this.createFeatureRecords();
		WeatherBox.setHasupdate(true);
		new FeatureRecord(map).play();
	}
	
	private void loadFeatureDefnition()
	{
		FeatureDefinitionBuilder builder = new FeatureDefinitionBuilder();
		FeatureDefinition definition = builder.buildFromCSV();
		if(definition != null)
		{
			PlayerUtilities.saveObjectToXMLFile(Settings.FEATURE_DEF_XML,definition);
		}
		new File(Settings.FEATURE_DEF_CSV).delete();
	}

	public void play() {
		//System.out.println("From feature player");
		this.createFeatureRecords();
		new FeatureRecord(map).play();
	}

	Map<Container, Content> map;

	public void createFeatureRecords()
	{
		try
		{
			// Getting feature container id from feature definition, then getting the container information from container.
			FeatureDefinition definition = FeatureDefinition.getInstance();
			
			if(definition == null)
				return;
			
			List<FeatureContainerDefinition> containerDefnition = definition.getDefinition();
			
			if(containerDefnition == null)
				return;
	
			//Mapping to container and content
			map = new HashMap<Container, Content>();
			Content content = null;
			//System.out.println("####Checking###"+containerDefnition.size());
			if(containerDefnition.size()>0)
			{
				for(FeatureContainerDefinition def : containerDefnition)
				{
					Container c = Player.getInstance().getContainer(def.getContainerID());
					if(c == null)
					{
						//System.out.println("Can not find the container "+def.getContainerID()+" in container.xml");
						continue;
					}
					
					if(def.getFeatureType().trim().equals("WEATHER"))
					{
							content = new Content(999999, ContentType.WEATHER, "WEATHER SERVICE", false, 0);
							map.put(c, content);
					}
					else if(def.getFeatureType().trim().equals("DATE"))
					{
							System.out.println(definition.getTextFormatMapper().get("format"));
							//System.exit(1);
							String format = definition.getTextFormatMapper().get("format").get("dateformat") == null ? "" : definition.getTextFormatMapper().get("format").get("dateformat").toString();
							TextContent txtContent = new TextContent(DateBox.getInstance().getFormattedDate(format));
	
							txtContent.setFormat(definition.getTextFormatMapper().get("format"));
							
							content = txtContent;
						    map.put(c, content);
					}
					else
					{
	
					}
				}
			}
		}
		catch(Exception e)
		{
			
		}
	}

	public static void main(String[] args) {
		FeaturePlayer.getInstance().createFeatureRecords();
	}
}
